$(document).ready(function() {
	initCart();
	initCommonFunction();
	layoutSetUp();
	topPageSetUp();
	productPageSetUp();
    showCart();
	userCommand();
});

function layoutSetUp() {
    $(".megamenu").megamenu();
	$(".scroll").click(function(event) {
		event.preventDefault();
		$('html,body').animate({
			scrollTop: $(this.hash).offset().top
		}, 1200);
	});
    $().UItoTop({
		easingType: 'easeOutQuart'
	});
}

function topPageSetUp() {
    $('#jquery-demo').slippry({
		slippryWrapper: '<div class="sy-box jquery-demo" />',
		adaptiveHeight: false,
		useCSS: false,
		autoHover: false,
		transition: 'fade'
	});
}

function productPageSetUp() {
	if (jQuery().fotorama != undefined) {
		fotoramaDiv = $('.fotorama').fotorama({
			width: 700,
			maxwidth: '100%',
			ratio: 16 / 9,
			allowfullscreen: true,
			nav: 'thumbs',
			captions: true,
			show: $(this).attr('data-start-index'),
		}).on('fotorama:show', function(e, fotorama, extra) {
			var option = selectedInSet.find('option[data-index=' + fotorama.activeIndex + ']');
			selectedInSet.val(option.val());
			changeSelectedProduct(option);
		});
		var selectedInSet = $('select#product-temp-detail');
		selectedInSet.change(function() {
			var option = $(this).find('option:selected');
			fotoramaDiv.data('fotorama').show({
				index: option.attr('data-index'),
				time: 0
			});
		});
		$('#product-qty').change(function() {
			var option = selectedInSet.find('option:selected');
			calculateOrderValue(option);
		});
	}
	if ($('#horizontalTab').length > 0) {
		$('#horizontalTab').easyResponsiveTabs({
			type: 'default',
			//Types: default, vertical, accordion
			width: 'auto',
			//auto or any width like 600px
			fit: true,
			// 100% fit in a container
			closed: 'accordion',
			// Start closed if in accordion view
			activate: function(event) { // Callback function if tab is switched
				var $tab = $(this);
				var $info = $('#tabInfo');
				var $name = $('span', $info);
				$name.text($tab.text());
				$info.show();
			}
		});
	}
	if ($('#verticalTab').length > 0) {
		$('#verticalTab').easyResponsiveTabs({
			type: 'vertical',
			width: 'auto',
			fit: true
		});
	}
}

function showCart() {
	var $cart = $('#cart');
	$('#clickme').click(function(e) {
		e.stopPropagation();
		if ($cart.is(":hidden")) {
			calculateCart();
			$cart.slideDown("fast");
		} else {
			$cart.slideUp("fast");
		}
	});
	$(document.body).click(function() {
		if ($cart.not(":hidden")) {
			$cart.slideUp("fast");
		}
	});
}

function changeSelectedProduct(option) {
	$('#product-id').val(option.val());
	$('#product-price').val(option.attr('data-price'));
	$('#order-temp-price').html(option.attr('data-price-format'));
	$('.product-description').addClass('hidden');
	$('.description-' + option.val()).removeClass('hidden');
	calculateOrderValue(option);
}

function calculateOrderValue(option) {
	var val = $('#product-qty').val() * option.attr('data-price');
	if (!isNaN(val)) {
		$('#order-temp-value').html(formatCurrency(val));
	} else {
		$('#order-temp-value').html('');
	}
}

function userCommand() {
	$('#frm-login').each(function() {
		$(this).find('input').keypress(function(e) {
			// Enter pressed?
			if (e.which == 10 || e.which == 13) {
				$('#btn-login').click();
			}
		});
	});
	//login
	$('#btn-login').click(function() {
		var form = $('#frm-login');
		$.ajax({
			url: form.attr('action'),
			type: 'POST',
			data: form.serialize(),
			cache: false,
			beforeSend: function() {
				$('#btn-login').attr('disabled', true);
			},
			success: function(data) {
				var alert_title = data.message;
				if (data.code != 1) {
					var alert_text = "<ul>" + data.details + "</ul>";
					$('div#flg_message').removeClass('alert-success').addClass('alert-danger').removeClass('hidden');
					$('div#flg_message .message-title').html(alert_title);
					$('div#flg_message .message-text').html(alert_text);
				} else {
					$('div#flg_message').removeClass('alert-danger').addClass('alert-success').removeClass('hidden');
					$('div#flg_message .message-title').html(alert_title);
					$('div#flg_message .message-text').hide();
					window.location = data.href;
				}
				$('#btn-login').attr('disabled', false);
			}
		});
	});
	//signup
	$('#btn-signup').click(function() {
		var form = $('#frm-signup');
		$.ajax({
			url: form.attr('action'),
			type: 'POST',
			data: form.serialize(),
			cache: false,
			beforeSend: function() {
				$('#btn-signup').attr('disabled', true);
			},
			success: function(data) {
				var alert_title = data.message;
				if (data.code != 1) {
					var alert_text = "<ul>" + data.details + "</ul>";
					$('div#flg_message').removeClass('alert-success').addClass('alert-danger').removeClass('hidden');
					$('div#flg_message .message-title').html(alert_title);
					$('div#flg_message .message-text').html(alert_text);
					$('#btn-signup').attr('disabled', false);
				} else {
					$('div#flg_message').removeClass('alert-danger').addClass('alert-success').removeClass('hidden');
					$('div#flg_message .message-title').html(alert_title);
					$('div#flg_message .message-text').hide();
				}
			}
		});
	});
}